package com.hmall.order.handler;



import com.hmall.order.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 声明拦截器
 */
@Slf4j
public class RequestHeaderContextInterceptor extends HandlerInterceptorAdapter {
    // private static final String HEAD_USER_NAME = "user_name";
    private static final String HEAD_TOKEN = "token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        initHeaderContext(request);
        return super.preHandle(request, response, handler);
    }

    private void initHeaderContext(HttpServletRequest request){

        String userInfo = request.getHeader("authorization");
        log.info(">>>>>>>拦截到api相关请求头<<<<<<<<"+userInfo);
        ThreadLocalUtil.setUserId(Long.valueOf(userInfo));
/*        JSONObject jsonObject = JSONObject.parseObject(userInfo);
        String user_name = (String) jsonObject.get("user_name");*/

//        new RequestHeaderContext.RequestHeaderContextBuild()
//                //.token(request.getHeader(HEAD_TOKEN))
//                .userName(user_name)
//                .bulid();
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        RequestHeaderContext.clean();
        super.postHandle(request, response, handler, modelAndView);
    }
}

